package com.mac.hosp_pre.admin.api;

import com.mac.hosp_pre.admin.model.req.*;
import com.mac.hosp_pre.admin.model.req.*;
import com.mac.hosp_pre.admin.service.DepartmentService;
import com.mac.hosp_pre.admin.service.HospitalService;
import com.mac.hosp_pre.admin.model.req.*;
import com.mac.hosp_pre.admin.model.resp.DeptRespDto;
import com.mac.hosp_pre.admin.model.resp.HospitalRespDto;
import com.mac.hosp_pre.admin.model.resp.ScheduleRespDto;
import com.mac.hosp_pre.reply.PageResult;
import com.mac.hosp_pre.reply.R;
import com.mac.hosp_pre.admin.service.HospitalConfigService;
import com.mac.hosp_pre.admin.service.ScheduleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "门户医院综合接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalConfigService hospitalConfigService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //删除排班
    @Operation(summary = "删除排班接口")
    @Parameters(value = {
            @Parameter(name = "hospCode", description = "医院code", required = true),
            @Parameter(name = "hosScheduleId", description = "排班id", required = true)
    })
    @PostMapping("schedule/remove")
    public R remove(String hospCode, String hosScheduleId) {
        //TODO 签名校验
        scheduleService.remove(hospCode,hosScheduleId);
        return R.success();
    }

    //查询排班接口
    @Operation(summary = "查询排班接口")
    @PostMapping("schedule/queryPage")
    public R queryPage(@RequestBody ScheduleQueryReqDto scheduleQuery) {
        //TODO 签名校验
        PageResult<ScheduleRespDto> pageSchedule = scheduleService.findPageSchedule(scheduleQuery);
        return R.success(pageSchedule);
    }

    //上传排班接口
    @Operation(summary = "上传排班接口")
    @PostMapping("schedule/add")
    public R add(@RequestBody ScheduleReqDto req) {
        //TODO 签名校验
        scheduleService.add(req);
        return R.success();
    }

    //删除科室接口
    @Operation(summary = "删除科室接口")
    @PostMapping("dept/remove")
    public R removeDepartment(String hospCode, String deptCode) {
        //TODO 签名校验
        departmentService.remove(hospCode,deptCode);
        return R.success();
    }

    //查询科室接口
    @Operation(summary = "查询科室接口")
    @PostMapping("dept/queryPage")
    public R queryPageDept(@RequestBody DeptQueryReqDto deptQuery) {
        PageResult<DeptRespDto> pageDepartment = departmentService.findPageDepartment(deptQuery);
        return R.success(pageDepartment);
    }

    @Operation(summary = "新增科室接口")
    @PostMapping("dept/add")
    public R saveDepartment(@RequestBody DeptReqDto req) {
      /*  //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalConfigService.getSignKey(req.getHospCode());

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!req.getSign().equals(signKeyMd5)) {
            throw new ServiceException(HospResultCodeEnum.SIGN_ERROR.getCode(), HospResultCodeEnum.SIGN_ERROR.SIGN_ERROR.getMsg());
        }*/
        departmentService.add(req);
        return R.success();
    }

    //查询医院
    @Operation(summary = "查询医院接口")
    @PostMapping("hospital/show")
    public R getHospital(String hospCode) {
        //调用service方法实现根据医院编号查询
        HospitalRespDto hospitalRespDto = hospitalService.getByHospCode(hospCode);
        return R.success(hospitalRespDto);
    }

    @Operation(summary = "新增医院接口")
    @PostMapping("hospital/add")
    public R saveHosp(@RequestBody HospitalReqDto req) {
        //TODO 签名校验
        if (!StringUtils.isEmpty(req.getHospCode())){
            //传输过程中“+”转换为了“ ”，因此我们要转换回来
            String logoData = req.getLogoData().replaceAll(" ","+");
            req.setLogoData(logoData);
        }
        //调用service的方法
        hospitalService.add(req);
        return R.success();
    }

}
